<template>
	<view class="box">

		<image src="../../static/image/pic6.png" mode="" class="top-banner"></image>
		<view class="propt-box">
			<view class="propt">
				身份信息是您申请 互助金的唯一凭证
			</view>
			<view class="propt-small">
				填写信息 生效最高35万保障
			</view>
		</view>
		<view class="user-box">
			<view class="info-box">
				<view class="info-list" v-for="(item,index) in infoList">
					<view class="section flex flai">
						<view class="section-title">关 　系</view>
						<view class="section-input flex1">{{item.relationName}}</view>
					</view>
					<view class="section flex flai">
						<view class="section-title">姓 　名</view>
						<input type="text" @input="nameValidate" :data-index="index" v-model="item.name" class="section-input flex1" :class="[item.isErrorName?'isError':'']"
						 placeholder="请输入真实姓名" />

					</view>
					<view class="section flex flai">
						<view class="section-title">身 份 证</view>
						<input @input="cardidValidate" :data-index="index" v-model="item.cardid"  maxlength="18" class="section-input flex1" placeholder="请输入身份证号" :class="[item.isErrorCard?'isError':'']" />

					</view>
				</view>
			</view>
			<view class="wring">
				<view class="wraning-propt flex flai fljc" v-if="isWrite">
					<image src="../../static/image/pic7.png" mode=""></image>
					<text>请至少完善一张卡片</text>
				</view>
				<view class="atonce" @click="nextPage">{{btnMsg}}</view>
				<view class="promise flex">
					<text>我承诺加入会员身体健康，无癌症或其他重大病史，点击</text>
					<navigator url="">【立即认证】</navigator><text>即代表您阅读并同意</text>
					<navigator :url="'/pages/protocol/healths?id='+item.id" v-for="item in planList" class="green" :key="item.id">《{{item.name}}》</navigator><text>条款。</text>
					<!-- <navigator url="" class="green">《健康要求》</navigator>和<navigator url="" class="green">《会员公约》</navigator> -->
				</view>

			</view>
		</view>
	
	</view>
</template>

<script>
	import api from '../../api/index.js'
	import {addNotScroll,removeNotScroll,isCardID,relationTypeDeal} from '../../static/common.js'
	export default {
		data() {
			return {
				btnMsg:'立即认证',
				infoList: [],
				isWrite: false,
				id:[],
				orderId:'',
				planList:[],
                 source:''//来源
			}
		},
		onLoad(opt) {
			this.source= opt.source;
			this.orderId = opt.orderId
		},
		onShow(){
			this.getList(this.orderId)
			
		},
		methods: {
			// 获取协议内容
			getJoinPlan (id){
				var data = {
					id:id,
					displayPoint:"P3"
				}
				api.cms.lookJoinPlanRule(data).then(res=>{
					if (res.code == 1) {
						this.planList = res.data
					}
				})
			},
			// 身份证输入的时候禁止输入非数字与字母
			cardidValidate(e) {
				var index = e.currentTarget.dataset.index
				var value = e.detail.value.replace(/[^\d|x|X_]/g,'')
				setTimeout(() => { 
					this.infoList[index].cardid = value
				}, 0)
			},
			// 姓名输入的时候禁止输入非字母和中文
			nameValidate(e) {
				var index = e.currentTarget.dataset.index
				var value = e.detail.value.replace(/[^\u4E00-\u9FA5A-Za-z]/g,'');

				setTimeout(() => { 
					this.infoList[index].name = value
					
				}, 0)
			},

			// 获取未激活保障人信息
			getList(id) {
				var opt = null
				if (id) {
					if(this.source==1){
						opt = {
							orderId:id
						}
					}else if(this.source==2){
						opt = {
							aidOrderId:id
						}
					}else{
						opt=''
					}
				}
				api.joinHellp.getBeneficiaryList(opt).then(res => {
					if (res.code == 1 && res.data.length>0) {
						
						var list = res.data[0].orderList
						
						var ids = []
					
						for (var k in list) {
							ids.push(list[k].id)
							list[k]['isErrorName'] = false
							list[k]['isErrorCard'] = false
							if (list[k]['name'] == null) {
								list[k]['name'] = ''
							}
							if (list[k]['cardid'] == null) {
								list[k]['cardid'] = ''
							}
							list[k]['relationName'] = relationTypeDeal(list[k]['relationType'])
						}
						this.id = ids
						this.infoList = list
						this.getJoinPlan(list[0].id)
					}
				})
			},
			// 返回加入互助页面
			backHome(){
				removeNotScroll()
				this.isShowMask = false
				uni.switchTab({
					url:'pages/joinPage/joinPage'
				})
			},
			// 去激活成功页面
			nextPage() {
				var list = this.infoList
				
				var nameRegex = /^[\u4e00-\u9fa5]+$/
				var isWrite = []
				for (var k in list) {
					if (list[k].cardid=='' || list[k].name=='') {
						isWrite.push(list[k])
					}
					if (isWrite.length==list.length) {
						uni.showToast({
							title: '请至少填写完一张卡片资料',
							icon: 'none'
						})
						return
					}
					
					if (list[k].cardid != '') {
						if (isCardID(list[k].cardid)==false) {
							this.$set(this.infoList[k],'isErrorCard',true)
							uni.showToast({
								title: '请输入正确的身份证号',
								icon: 'none'
							})
							return
						} else {
							this.$set(this.infoList[k],'isErrorCard',false)
						}
					}
					if (list[k].name !== '') {
						var result = nameRegex.test(list[k].name)
						if (result===false){
							this.infoList[k]['isErrorName'] = true
							uni.showToast({
								title: '请输入正确的姓名',
								icon: 'none'
							})
							return
					} else {
						this.infoList[k]['isErrorName'] = false
					}
					}
				}
				var parms = []
				var list = this.infoList
				for (var y in list) {
							if (list[y].name!=''){
					
							if (list[y].cardid==''){
								this.$set(this.infoList[y],'isErrorCard',true)
								uni.showToast({
								title: '请将信息填写完整',
								icon: 'none'
							})
							return
						}
					} 
					if (list[y].cardid!=='') {
						if (list[y].name==''){
							this.$set(this.infoList[y],'isErrorName',true)
								uni.showToast({
								title: '请将信息填写完整',
								icon: 'none'
							})
							return
						}
					}
					list[y]['isErrorName'] = false
					list[y]['isErrorCard'] = false
					this.$set(this.infoList[y],'isErrorName',false)
					this.$set(this.infoList[y],'isErrorCard',false)
					parms.push({name:list[y].name,cardid:list[y].cardid,relationType:list[y].relationType,personId:list[y].personId,id:list[y].id})
				}
				api.joinHellp.actived(JSON.stringify(parms)).then(res=>{
			
					if (res.code == 1) {
						if (res.msg=='实名认证失败'){
							uni.showToast({
								title:"实名认证失败",
								icon:'none'
							})
							setTimeout(()=>{
								uni.navigateTo({
									url: '/pages/activateSuccess/activateSuccess?id='+JSON.stringify(this.id) +'&orderId='+this.orderId
								});
							},2000)
						} else {
							uni.navigateTo({
								url: '/pages/activateSuccess/activateSuccess?id='+JSON.stringify(this.id) +'&orderId='+this.orderId
							});
						}
						
					}
				})
			},
		}
	}
</script>

<style>
	@import "../../static/css/common.css";
	@import "./guarantee.css";
</style>
